package service

import (
	"context"
	"github.com/micro/go-micro/v2/errors"
	"github.com/silenceper/wechat/v2/miniprogram/qrcode"
	"micro-grpc-server/common"
	pb "micro-grpc-server/services/wechat/proto"
	"strconv"
)

// 获取微信小程序码
func (s *WechatService) WechatCode(ctx context.Context, rq *pb.WechatQrcodeRequest, resp *pb.WechatQrcodeResponse) (err error) {
	miniProgram := s.dao.GetMini(rq.Scenario)
	if miniProgram == nil {
		return errors.BadRequest(s.conf.ServerName, "%s 配置不存在", rq.Scenario)
	}
	code := qrcode.QRCoder{
		Path:      rq.Path,
		Page:      rq.Page,
		Width:     int(rq.Width),
		Scene:     rq.Scene,
		IsHyaline: rq.IsHyaline,
	}

	if len(rq.LineColor) > 0 {
		var r, g, b = common.ColorToRgb(rq.LineColor)
		code.LineColor = qrcode.Color{R: strconv.Itoa(r), G: strconv.Itoa(g), B: strconv.Itoa(b)}
		code.AutoColor = false
	}

	resp.Image, err = miniProgram.GetQRCode().GetWXACodeUnlimit(code)
	return
}
